<template>
	<view>
		<!-- <u-navbar title="我的家庭" :placeholder="true" leftIcon=" " :bgColor="bgColor" :titleStyle="titleStyle"></u-navbar> -->
		<headerinfo></headerinfo>
		<!-- <headertop :inv="4"></headertop> -->
		<view class="label">
			我的家庭
		</view>
		<view class="listBox">
			<view class="list" :class="'list'+index"
				:style="[{background:item.useColorAsBg?`url(${item.familyBackground+'?x-oss-process=image/resize,h_150,w_150'})  no-repeat center top`:item.familyBackground}]"
				v-for="(item,index) in list" :key="index" @click="showCaozuo(item,index)">
				<view class="title">
					{{item.familyName}}
				</view>
				<!-- <view class="desc">
					{{item.manageUsername}}
				</view> -->
			</view>
			
			<view class="list" style="height: 0;"></view>
			<view class="list" style="height: 0;"></view>
			<view class="list" style="height: 0;"></view>
			<view class="list" style="height: 0;"></view>
		</view>
		
		<view class="btn" @click="toPage('/pages/tabbar/family/createFamily?type=1')">
				添加家庭
		</view>
		<u-action-sheet @close="show = false" @select="selectClick" :closeOnClickOverlay="true"
			:closeOnClickAction="true" :actions="sheetlist" cancelText="取消" :show="show"></u-action-sheet>

		<!-- <u-popup :show="showChangeName" :round="10" mode="center" @close="showChangeName = false">
			<view class="ChangeName">
				<view class="title">
					<span class="red">*</span> 当前家庭：{{checkFamily.familyName}}
				</view>
				<input type="text" v-model="newFamilyName" placeholder="请输入新家庭名称">
				<view class="btn" @click="submitChangeName">
					提交
				</view>
			</view>
		</u-popup> -->
	</view>
</template>

<script>
	export default {
		components: {
			
		},
		data() {
			return {
				titleStyle: {
					color: '#fff'
				},
				bgColor: '#de4849',
				list: [],
				newFamilyName: '',
				// 操作栏
				sheetlist: [{
					name: '查看家庭树',
					color: '#ff752f'
				}, {
					name: '更改家庭信息'
				}, {
					name: '删除家庭'
				}, {
					name: '家族祠堂'
				}],
				show: false,
				checkFamily: {
					family: {
						familyName: ''
					}
				},
				checkIndex: -1,
				username: uni.getStorageSync('username') ? uni.getStorageSync('username') : ''
			};
		},
		onShow() {
			this.getData();
			this.username = uni.getStorageSync('username') ? uni.getStorageSync('username') : ''
		},
		mounted() {},
		methods: {
			delMyFamily() {
				console.log(this.checkFamily)
				let name = this.checkFamily.familyName;
				let familyId = this.checkFamily.docId;
				uni.showModal({
					title: '提示',
					content: `是否确认删除 ${name} 家庭？`,
					success: res => {
						if (res.confirm) {
							this.$api
								.deleteFamilyById({
									familyId
								})
								.then(res => {
									this.list.splice(this.checkIndex, 1)
									// this.getData();
								});
						}
					}
				});
			},
			// submitChangeName() {
			// 	if (this.newFamilyName == '') {
			// 		return this.$Show.show('新家庭名称不能为空')
			// 	}

			// 	let data = {
			// 		familyId: this.checkFamily.docId,
			// 		newFamilyName: this.newFamilyName
			// 	}

			// 	this.$api.updateFamilyName(data).then(res => {
			// 		this.getData();
			// 		this.showChangeName = false
			// 		this.newFamilyName = ''
			// 	});
			// },
			showCaozuo(item, index) {
				console.log(item)
				this.checkFamily = item
				this.checkIndex = index
				if (this.username != this.checkFamily.manageUsername) {
					this.sheetlist = [{
						name: '查看家庭树',
						color: '#ff752f'
					}, {
						name: '更改家庭信息',
						disabled: true
					}, {
						name: '删除家庭',
						disabled: true
					},{
						name: '家族祠堂'
					}
				]
				} else {
					this.sheetlist = [{
						name: '查看家庭树',
						color: '#ff752f'
					}, {
						name: '更改家庭信息',
					}, {
						name: '删除家庭',
					},{
						name: '家族祠堂'
					}]
				}
				this.show = true
			},
			selectClick(e) {
				console.log(e)
				if (e.name == '查看家庭树') {
					let id = this.checkFamily.docId
					let manageUsername = this.checkFamily.manageUsername
					let familyBackground = this.checkFamily.familyBackground
					let useColorAsBg = this.checkFamily.useColorAsBg ? 1 : 2
					this.toPage(
						`/pages/tabbar/family/familylist?id=${id}&manageUsername=${manageUsername}&familyBackground=${familyBackground}&useColorAsBg=${useColorAsBg}`
						)
				} else if (e.name == '更改家庭信息') {
					console.log(this.checkFamily)
					let data = JSON.stringify(this.checkFamily)
					uni.navigateTo({
						url: `/pages/tabbar/family/createFamily?data=${data}&type=1`
					})
					// this.showChangeName = true
				} else if (e.name == '删除家庭') {
					this.delMyFamily()
				}else if (e.name == '家族祠堂') {
					console.log('this.checkFamily')
					console.log()
					
					if(this.checkFamily.museumId){
						//有祠堂
						uni.navigateTo({
							url:`/pagesobit/obit/citang?id=${this.checkFamily.museumId}`
						})
					}else{
						uni.navigateTo({
							url:`/pagesobit/obit/create?docId=${this.checkFamily.docId}`
						})
					}
					
				}
			},
			toPage(url) {
				console.log(url)
				uni.navigateTo({
					url
				})
			},
			getData() {
				this.$api.myFamilies().then(res => {
					if (res && res.length > 0) {
						this.list = res;
						console.log(this.list)
					}
				});
			},
		}
	};
</script>


<style lang="scss">
	
		
	.label{
		width: 60%;
		margin: 30px auto;
		font-size: 24px;
	}
	.listBox {
		display: flex;
		justify-content: space-between;
		box-sizing: border-box;
		width: 60%;
		margin-left: 2.5%;
		margin-top: 20rpx;
		flex-wrap: wrap;
		margin: 30px auto;
		padding: 20px;
		box-shadow: 0 0 3px #ccc;
		border-radius: 20rpx;
		position: relative;
		
		.list {
			width: 23%;
			height: 470rpx;
			box-shadow: 0 0 8px #e6e6e6;
			border-radius: 10rpx;
			margin-bottom: 20rpx;
			background-image: linear-gradient(135deg, #FF9D6C 10%, #BB4E75 100%);
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			background-size: cover !important;

			.title {
				color: #fff;
				font-size: 30rpx;
			}

			.desc {
				color: #fff;
				font-size: 26rpx;
				margin-top: 20rpx;
			}
		}

		.list::after {
			content: '';
			width: 31%;
		}

		.lists {
			width: 23%;
			height: 470rpx;
			box-shadow: 0 0 4px #a8a5a5;
			border-radius: 10rpx;
			margin-bottom: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;

			.iconfont {
				font-size: 48rpx;
				color: #acacac;
			}

			.desc {
				color: #acacac;
				font-size: 28rpx;
				margin-top: 20rpx;
			}
		}
	}

	.ChangeName {
		padding: 50rpx 30rpx;
		box-sizing: border-box;
		width: 650rpx;
		border-radius: 10rpx;

		.title {
			font-size: 30rpx;
			margin-bottom: 20rpx;

			.red {
				color: red;
			}
		}

		input {
			background-color: #ededed;
			height: 85rpx;
			border-radius: 10rpx;
			padding: 20rpx;
			font-size: 30rpx;

			width: 100%;
		}

		
	}
	
	.btn {
		width: 500rpx;
		height: 95rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		background-color: #de4849;
		border-radius: 10rpx;
		font-size: 28rpx;
		margin: 0 auto;
		margin-top: 40rpx;
		cursor: pointer;
	
	}
</style>